package com.zyf.system.until;

/**
 * @author 张云帆
 * 2025/3/25 11:45
 */
import lombok.extern.log4j.Log4j2;

import javax.servlet.http.HttpServletRequest;

@Log4j2
public class IpUtil {

    public static String getIpAddr(HttpServletRequest request) {
        String ipFromNginx = getHeader(request, "X-Real-IP");
        if("".equals(ipFromNginx)){
            return request.getRemoteAddr();
        }else{
            return ipFromNginx;
        }
    }

    private static String getHeader(HttpServletRequest request, String headName) {
        String value = request.getHeader(headName);
        if(value==null||"".equals(value)||"unknown".equals(value)){
            return "";
        }else{
            return value;
        }
    }

}
